<?php
class searchFlight
{
	public $origin_airport = null;
	public $destination_airport = null;
	public $leg = null;
	public $actual_departure_time = null;
	public $actual_arrival_time = null;
	public $flight_schedule = null;
	public $actual_duration = null;
	public $airline = null;

	public function initActualTime($time)
	{

		// take flightSchedule and calculate actual departure and arrival time

		if($this->flight_schedule == null) return false;

		//*********** Calculate actual departure

		// get actual day
		$departure_d = date('Y-m-d',$time);

		// get actual hour
		$departure_h = date('h:i:s',strtotime($this->flight_schedule->getDepartureTime()));

		$this->actual_departure_time = strtotime($departure_d.' '.$departure_h);

		//*********** Calculate actual arrival

		$this->actual_duration = strtotime($this->flight_schedule->getArrivalTime()) - strtotime($this->flight_schedule->getDepartureTime());

		$this->actual_arrival_time = $this->actual_departure_time + $this->actual_duration;

		$this->getOrCreateLeg(0,false);
		
		return true;

	}

	public function getOrCreateLeg($new_places_taken = 0, $create_leg = true)
	{
		if($this->flight_schedule == null || $this->actual_departure_time == null) return null;
		if($this->leg != null) return $this->leg;
		else
		{

			// check if leg exists in database

			$leg = Doctrine_Core::getTable('AoiLeg')
							->createQuery('l')
							->where('l.flight_schedule_id = ?', $this->flight_schedule->getId())
							->andWhere('l.actual_departure_time >= ?', date('Y-m-d 00:00:00', $this->actual_departure_time))
							->andWhere('l.actual_departure_time <= ?', date('Y-m-d 23:59:59', $this->actual_departure_time))
							->fetchOne();

			if(!$leg && $create_leg)
			{
				// else create leg

				$leg = new AoiLeg();
				$leg->setFlightScheduleId($this->flight_schedule->getId());
				$leg->setFlightCode($this->flight_schedule->getFlightCode());
				$leg->setName($this->flight_schedule->getFlightCode().' '.date('Ymd', $this->actual_departure_time).' '.$this->flight_schedule->getAoiOriginAirport()->getCode().'-'.$this->flight_schedule->getAoiDestinationAirport()->getCode());
		    $leg->setActualDepartureTime(date('Y-m-d h:i:s', $this->actual_departure_time));
		    $leg->setActualArrivalTime(date('Y-m-d h:i:s', $this->actual_arrival_time));

		    $plane = $this->flight_schedule->getAoiPlane();

		    $leg->setCapacity($plane->getCapacity());
		    $leg->setPlacesTaken($new_places_taken);
		    $leg->setPlacesLeft($plane->getCapacity() - $new_places_taken);
		    $leg->save();
			}
			else
			{
				if($new_places_taken > 0)
				{
					$places_left  = $leg->getPlacesLeft();
					$places_taken = $leg->getPlacesTaken();
					$leg->setPlacesLeft($places_left - $new_places_taken);
					$leg->setPlacesTaken($places_taken + $new_places_taken);
		    	$leg->save();
				}
			}
	    $this->leg = $leg;
	    return $this->leg;
		}
	}

	public function hasPlacesLeft($places_wanted)
	{
		// if no leg created yet, all places available !
		if($this->leg == null || !$this->leg) return true;
		else
		{
			return ($this->leg->getPlacesLeft() >= $places_wanted);
		}
	}


	public function getPlacesLeft()
	{
		// if no leg created yet, all places available !
		if($this->leg != null) return $this->leg->getPlacesLeft();
		else if($this->flight_schedule) return $this->flight_schedule->getAoiPlane()->getCapacity();
		else return 99999999;
	}

	public function getPlane()
	{
		if($this->flight_schedule == null || !$this->flight_schedule) return null;
		else
		{
			return $this->flight_schedule->getAoiPlane();
		}
	}

	public function getAirline()
	{
		if($this->airline != null) return $this->airline;
		else if($this->flight_schedule != null)
		{
			$this->airline = Doctrine_Core::getTable('AoiAirline')->findOneById($this->flight_schedule->getAirlineId());
			return $this->airline;
		}
		return null;
	}

	public function getOriginAirport()
	{
		if($this->origin_airport != null) return $this->origin_airport;
		else if($this->flight_schedule != null)
		{
			$this->origin_airport = Doctrine_Core::getTable('AoiAirport')->findOneById($this->flight_schedule->getOriginAirportId());
			return $this->origin_airport;
		}
		return null;
	}

	public function getDestinationAirport()
	{
		if($this->destination_airport != null) return $this->destination_airport;
		else if($this->flight_schedule != null)
		{
			$this->destination_airport = Doctrine_Core::getTable('AoiAirport')->findOneById($this->flight_schedule->getDestinationAirportId());
			return $this->destination_airport;
		}
		return null;
	}

	public function getActualDepartureTime()
	{
		return $this->actual_departure_time;
	}

	public function getActualArrivalTime()
	{
		return $this->actual_arrival_time;
	}
	
	public function getActualDuration()
	{
		return $this->actual_duration;
	}
	
	public function getFlightSchedule()
	{
		return $this->flight_schedule;
	}

	public function setFlightSchedule($flight_schedule)
	{
		$this->flight_schedule = $flight_schedule;
	}

	public function getLeg()
	{
		return $this->leg;
	}

}         


 
